package com.SoapClasses;


import java.io.IOException;
import java.net.SocketTimeoutException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import com.Data.User;
import com.Managers.LogManagerClass;


public class SoapPost_Follows {

    private static final String TAG = "SoapPost_Followers";

    SoapSerializationEnvelope envelope;

    PropertyInfo pi;

    SoapObject Request;

    public String addFollow(String[] data) {

        String METHOD_NAME = "addFollow";
        String SOAP_ACTION = "http://ShareUp.com/addFollow";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        String info = "";
        for (int i = 0; i < data.length; i++) {
            info += data[i] + "@";
        }
        Request.addProperty("data", info);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String x = " ";
            for (StackTraceElement line : stack) {
                x += line.toString();
            }
            Log.e(TAG + ": ", x);
            LogManagerClass.writeLog(TAG + ": " + x);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        // IF EVERYTHING WENT OKAY IT GOING TO RETURN -1
        return response.toString();
    }

    public String deleteFollow(String[] data) {

        String METHOD_NAME = "deleteFollow";
        String SOAP_ACTION = "http://ShareUp.com/deleteFollow";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        String info = "";
        for (int i = 0; i < data.length; i++) {
            info += data[i] + "@";
        }
        Request.addProperty("data", info);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (Exception x) {
            response = x.toString();
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
        }

        // IF EVERYTHING WENT OKAY IT GOING TO RETURN -1
        return response.toString();
    }

    public User[] findFollowers(String name) {

        //
        String METHOD_NAME = "findFollowers";
        String SOAP_ACTION = "http://ShareUp.com/findFollowers";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        Request.addProperty("name", name);
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        /*
         * Call the web service and retrieve result ...
         */
        Object response = null;
        //
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
            //
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            // ERROR -10 CONNECTION TIMEOUT
            User uTimeOut = new User();
            uTimeOut.setID(-10);
            User[] us = new User[1];
            us[0] = uTimeOut;
            return us;
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String x = " ";
            for (StackTraceElement line : stack) {
                x += line.toString();
            }
            Log.e(TAG + ": ", x);
            LogManagerClass.writeLog(TAG + ": " + x);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
        }

        return getUserInfo(response, true);
    }

    public User[] getFriends(int IDUSER) {

        //
        String METHOD_NAME = "getFollowers";
        String SOAP_ACTION = "http://ShareUp.com/getFollowers";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        Request.addProperty("id", IDUSER);
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        /*
         * Call the web service and retrieve result ...
         */
        Object response = null;
        //
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
            //
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", "TIMEOUT");
            LogManagerClass.writeLog(TAG + ": " + "TIMEOUT");
            // ERROR -10 CONNECTION TIMEOUT
            User uTimeOut = new User();
            uTimeOut.setID(-10);
            User[] us = new User[1];
            us[0] = uTimeOut;
            return us;
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String x = " ";
            for (StackTraceElement line : stack) {
                x += line.toString();
            }
            Log.e(TAG + ": ", x);
            LogManagerClass.writeLog(TAG + ": " + x);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
        }
        //
        //
        return getUserInfo(response, false);
    }

    public String isFollowed(String[] data) {

        String METHOD_NAME = "isFollowed";
        String SOAP_ACTION = "http://ShareUp.com/isFollowed";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        String info = "";
        for (int i = 0; i < data.length; i++) {
            info += data[i] + "@";
        }
        Request.addProperty("data", info);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (Exception x) {
            response = x.toString();
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
        }

        return response.toString();
    }

    private User[] getUserInfo(Object sp, boolean searching) {

        String RES = sp.toString();

        try {
            JSONArray jsonArray = new JSONArray(RES);

            User[] uss = new User[jsonArray.length()];

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jo = new JSONObject(jsonArray.getString(i));
                User us = new User();
                int ID = Integer.parseInt(jo.getString("ID"));
                String RealName = jo.getString("RealName");
                String Email = jo.getString("Email");
                // String Phone = jo.getString("Phone");
                String ProfilePicture = jo.getString("ProfilePicture");
                int Sex = Integer.parseInt(jo.getString("Sex"));
                String Description = jo.getString("Description");
                int Follow = Integer.parseInt(jo.getString("Follow"));
                int Privacy = jo.getInt("Privacy");
                //
                us.setID(ID);
                us.setEmail(Email);
                us.setIsFollowingMe(Follow);
                us.setRealName(RealName);
                us.setSex(Sex);
                us.setProfilePicture(ProfilePicture);
                us.setDescription(Description);
                us.setPrivacy(Privacy);

                uss[i] = us;
            }
            return uss;

        }
        catch (JSONException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());

            return null;
        }

    }
}
